package com.sto.stoapp.util;

import android.annotation.SuppressLint;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@SuppressLint("SimpleDateFormat")
public class DateUtil {
	/** yyyy-MM-dd HH:mm:ss字符串 */
	public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

	public static final String DEFAULT_DATE_FORMAT = "yyyyMMdd";

	/**
	 * 获取yyyy-MM-dd HH:mm:ss 时间格式d的时间
	 * 
	 * @return
	 */
	public static String getDateTime() {
		SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
		return format.format(new Date());
	}

	public static String getYestdayDateTime() {
		Calendar mCalendar = Calendar.getInstance();
		mCalendar.add(Calendar.DATE, -1);
		SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
		return format.format(mCalendar.getTime());
	}

	/**
	 * 获取yyyyMMdd 时间格式的时间
	 * 
	 * @return
	 */
	public static String getDate() {
		SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
		return format.format(new Date());
	}

	/**
	 * 获取yyyyMMddHHmmss 时间格式的时间
	 * 
	 * @return
	 */
	public static String getLongDateTime() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		return format.format(new Date());
	}

	public static String getTodayDate(String separator) {
		Calendar c = Calendar.getInstance();// 可以对每个时间域单独修改

		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		int date = c.get(Calendar.DATE);
		month = month + 1;
		String monthStr = "" + month;
		String dateStr = "" + date;
		if (month < 10) {
			monthStr = "0" + monthStr;
		}
		if (date < 10) {
			dateStr = "0" + dateStr;
		}
		String s = year + "%s" + monthStr + "%s" + dateStr;
		return String.format(s, separator, separator);
	}

	public static String getNowTime(String separator) {
		Calendar c = Calendar.getInstance();// 可以对每个时间域单独修改
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int minute = c.get(Calendar.MINUTE);
		String min = "" + minute;
		if (minute < 10) {
			min = "0" + minute;
		}
		String s = hour + "%s" + min;
		return String.format(s, separator, separator);
	}

	/**
	 * 
	 * @param pattern
	 *            SimpleDateFormat
	 * @param time
	 * @return
	 */
	public static long getLongTime(String pattern, String time) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);

		try {
			return sdf.parse(time).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
			return 0;
		}
	}

}
